﻿
using OrderApp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OrderForm {
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            orderBindingSource.DataSource = OrderService.GetAllOrders();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FormEdit form2 = new FormEdit(new Order());
            if (form2.ShowDialog() == DialogResult.OK)
            {
                OrderService.AddOrder(form2.CurrentOrder);
                QueryAll();
            }
        }

        private void querybyGoButton_Click(object sender, EventArgs e)
        {
            orderBindingSource.DataSource = OrderService.QueryOrdersByGoodsName(txtValue.Text);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            orderBindingSource.DataSource = OrderService.QueryOrdersByCustomerName(txtValue.Text);
        }
        private void QueryAll()
        {
            orderBindingSource.DataSource = OrderService.GetAllOrders();
            orderBindingSource.ResetBindings(false);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            EditOrder();
        }
        private void dbvOrders_DoubleClick(object sender, EventArgs e)
        {
            EditOrder();
        }
        private void EditOrder()
        {
            Order order = orderBindingSource.Current as Order;
            if (order == null)
            {
                MessageBox.Show("请选择一个订单进行修改");
                return;
            }
            order = OrderService.GetOrder(order.Id); //查询出最新的订单信息
            FormEdit form2 = new FormEdit(order, true);
            if (form2.ShowDialog() == DialogResult.OK)
            {
                OrderService.UpdateOrder(form2.CurrentOrder);
                QueryAll();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Order order = orderBindingSource.Current as Order;
            if (order == null)
            {
                MessageBox.Show("请选择一个订单进行删除");
                return;
            }
            OrderService.RemoveOrder(order.Id);
            QueryAll();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            DialogResult result = saveFileDialog1.ShowDialog();
            if (result.Equals(DialogResult.OK))
            {
                String fileName = saveFileDialog1.FileName;
                OrderService.Export(fileName);
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result.Equals(DialogResult.OK))
            {
                String fileName = openFileDialog1.FileName;
                OrderService.Import(fileName);
                QueryAll();
            }
        }





        private void FormMain_Load(object sender, EventArgs e)
        {
        }

        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void showAllButton_Click(object sender, EventArgs e)
        {
            QueryAll();
        }
    }
}
